﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo.Services.Services.Impl.Managers;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Ext.Net;

namespace Demo.Web.Catalogs
{
    public partial class AreaEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = 0;
                if (Request["Id"] != null)
                {
                    id = Convert.ToInt32(Request["Id"]);
                    SetEditData(id);
                }
            }
        }

        private void SetEditData(int id)
        {
            AreaManager manager;
            AreaInfo resourceTypeInfo;

            try
            {
                Logger.Info();
                manager = new AreaManager();
                resourceTypeInfo = manager.GetAreaById(id);
                txtAreaID.Text = resourceTypeInfo.Id.ToString();
                txtDescription.Text = resourceTypeInfo.Description;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        protected void Guardar(object sender, DirectEventArgs e)
        {
            AreaManager areaManager;
            AreaInfo areaInfo;
            try
            {
                int id = 0;
                areaManager = new AreaManager();
                areaInfo = GetData();
                if (Request["Id"] != null)
                {
                    id = Convert.ToInt32(txtAreaID.Text);
                    areaManager.UpdateArea(areaInfo);
                }
                else
                {
                    areaManager.SaveArea(areaInfo);
                }
                X.Js.AddScript("parentAutoLoadControl.hide();");
                X.Js.AddScript("parent.Ext.getCmp('btnRefresh').fireEvent('click');");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        private AreaInfo GetData()
        {
            AreaInfo areaType = new AreaInfo();
            int id = 0;
            if (Request["Id"] != null)
            {
                id = Convert.ToInt32(txtAreaID.Text);
            }
            areaType.Id = id;
            areaType.Description = txtDescription.Text;

            return areaType;
        }

        protected void CheckName(object sender, RemoteValidationEventArgs e)
        {
            int id = txtAreaID.Text == string.Empty ? 0 : Convert.ToInt32(txtAreaID.Text);
            TextField txtName = (TextField)sender;
            AreaManager areaManager = new AreaManager();
            bool isValid = areaManager.CheckName(id, txtName.Text);

            if (isValid)
            {
                e.Success = true;
            }
            else
            {
                e.Success = false;
                e.ErrorMessage = "Nombre de Area Existente";
            }

            System.Threading.Thread.Sleep(1000);
        }

    }
}